﻿#region Usings

using System;
using System.Xml;

#endregion

namespace Kazkommertsbank.Framework.Common
{
    public static class XmlWriterExtensions
    {
        public static XmlWriter WriteElement(this XmlWriter writer, string elementName, Action<XmlWriter> writeContent)
        {
            Utils.ArgumentNotNull(writer, "writer");
            Utils.ArgumentNotNull(writeContent, "writeContent");
            writer.WriteStartElement(elementName);
            writeContent(writer);
            writer.WriteEndElement();
            return writer;
        }

        public static XmlWriter WriteElement(this XmlWriter writer, string elementName, Action<XmlWriter, object[]> writeContent, params object[] args)
        {
            Utils.ArgumentNotNull(writer, "writer");
            Utils.ArgumentNotNull(writeContent, "writeContent");
            writer.WriteStartElement(elementName);
            writeContent(writer, args);
            writer.WriteEndElement();
            return writer;
        }

        public static void WriteBooleanAttribute(this XmlWriter writer, string localName, bool? value)
        {
            if(value.HasValue)
                writer.WriteAttributeString(localName, XmlConvert.ToString(value.Value));
        }

        public static void WriteDoubleAttribute(this XmlWriter writer, string localName, double? value)
        {
            if(value.HasValue)
                writer.WriteAttributeString(localName, XmlConvert.ToString(value.Value));
        }

        public static void WriteInt32Attribute(this XmlWriter writer, string localName, int? value)
        {
            if(value.HasValue)
                writer.WriteAttributeString(localName, XmlConvert.ToString(value.Value));
        }

        public static void WriteUInt32Attribute(this XmlWriter writer, string localName, uint? value)
        {
            if(value.HasValue)
                writer.WriteAttributeString(localName, XmlConvert.ToString(value.Value));
        }

        public static void WriteStringAttribute(this XmlWriter writer, string localName, string value)
        {
            if(!String.IsNullOrEmpty(value))
                writer.WriteAttributeString(localName, value);
        }

        public static void WriteEnumAttribute<TEnum>(this XmlWriter writer, string localName, TEnum enumValue) where TEnum : struct
        {
            string value = Enum<TEnum>.GetName(enumValue);
            if(string.IsNullOrEmpty(value))
                writer.WriteAttributeString(localName, value);
        }

        public static void WriteBooleanElement(this XmlWriter writer, string elementName, bool? value)
        {
            if(value.HasValue)
                writer.WriteElementString(elementName, XmlConvert.ToString(value.Value));
        }

        public static void WriteDoubleElement(this XmlWriter writer, string elementName, double? value)
        {
            if(value.HasValue)
                writer.WriteElementString(elementName, XmlConvert.ToString(value.Value));
        }

        public static void WriteInt32Element(this XmlWriter writer, string elementName, int? value)
        {
            if(value.HasValue)
                writer.WriteElementString(elementName, XmlConvert.ToString(value.Value));
        }

        public static void WriteUInt32Element(this XmlWriter writer, string elementName, uint? value)
        {
            if(value.HasValue)
                writer.WriteElementString(elementName, XmlConvert.ToString(value.Value));
        }

        public static void WriteStringElement(this XmlWriter writer, string elementName, string value)
        {
            if(!String.IsNullOrEmpty(value))
                writer.WriteElementString(elementName, value);
        }

        public static void WriteEnumElement<TEnum>(this XmlWriter writer, string elementName, TEnum enumValue) where TEnum : struct
        {
            string value = Enum<TEnum>.GetName(enumValue);
            if(string.IsNullOrEmpty(value))
                writer.WriteElementString(elementName, value);
        }
    }
}